//
//  MianTabBarViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/4/1.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class MianTabBarViewController: UITabBarController, UITabBarControllerDelegate,UIAlertViewDelegate,MBProgressHUDDelegate {
    
    var hud: MBProgressHUD!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1212 - 标题都写回对应的VC里面了
        
        // Do any additional setup after loading the view.
//        self.view.backgroundColor = UIColor.init(hexString: "f6f6f6", alpha: 1.0)
//        self.navigationController?.navigationBar.barTintColor = barColor
//        let font = UIFont.boldSystemFont(ofSize: 20)
//        let dic: Dictionary = [NSFontAttributeName : font, NSForegroundColorAttributeName : UIColor.white]
//        self.navigationController?.navigationBar.titleTextAttributes = dic;
//        self.title = "管家"
//        self.delegate = self
        
        initTabBarItems()
        self.selectedIndex = 0
        
        //1212- 这两个响应通知方法放回对应的VC里面了
//        NotificationCenter.default.addObserver(self, selector: #selector(MianTabBarViewController.MainRightClick(_:)), name: NSNotification.Name(rawValue: "MainRightClick"), object: nil)
        
//        NotificationCenter.default.addObserver(self, selector: #selector(MianTabBarViewController.MeClick(_:)), name: NSNotification.Name(rawValue: "MeClick"), object: nil)
        
    }
    
    //MARK: - init tabvbar
    
    func initTabBarItems() {
 
        #if HSEDUMANAGE || TXEDUMANAGE
            addChildViewController(controller: OfficeViewController(), barTitle: "办公", navTitleTitle: "办公管家", image: #imageLiteral(resourceName: "huibangong"), selectedImage: #imageLiteral(resourceName: "lanbangong"))
        #endif
        
        #if EDUMANAGE || HSEDUMANAGE || TXEDUMANAGE
            addChildViewController(controller: MainViewController(), barTitle: "家校", navTitleTitle: "家校管家", image: #imageLiteral(resourceName: "32classb"), selectedImage: #imageLiteral(resourceName: "32classg"))
            addChildViewController(controller: MeViewController(), barTitle: "我", navTitleTitle: "我", image: #imageLiteral(resourceName: "32meb"), selectedImage: #imageLiteral(resourceName: "32meg"))
        #endif
        
    }
    
    func addChildViewController(controller: UIViewController, barTitle: String, navTitleTitle: String, image: UIImage, selectedImage: UIImage) {
        
        let baseNav = BaseNavViewController(rootViewController: controller)
        
        baseNav.tabBarItem.image = image
        baseNav.tabBarItem.selectedImage = selectedImage
        
        baseNav.tabBarItem.title = barTitle
        controller.navigationItem.title = navTitleTitle
        
        self.addChildViewController(baseNav)
    }
    
    /*
    //MARK: - MEClick action
    
    func MeClick(_ notification: Notification) {
        let dic: NSDictionary = (notification as NSNotification).userInfo! as NSDictionary
        let target = dic.object(forKey: "target") as! String
        switch target {
        case "关于我们":
            let aboutUS = AboutUsViewController()
            self.navigationController?.pushViewController(aboutUS, animated: true)
            break
        case "意见反馈":
            let opinion = OpinionViewController()
            self.navigationController?.pushViewController(opinion, animated: true)
            break
        case "举报投诉":
            let report = ReportViewController()
            self.navigationController?.pushViewController(report, animated: true)
            break
        case "清理缓存":
            let alert: UIAlertView = UIAlertView.init(title: "清理缓存", message: "缓存大小为\(String(format: "%.2f",getCachSize()))M,确定清理吗？", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "确定")
            alert.show()
            break
        case "修改密码":
            let alert: UIAlertController = UIAlertController.init(title: "验证原密码", message: "为保障你的数据安全，修改密码前请填写原密码。", preferredStyle: UIAlertControllerStyle.alert)
            alert.addTextField(configurationHandler: {
                (textField: UITextField!) -> Void in
                textField.placeholder = "请输入原密码"
            })
            let cancleAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
            let okAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.default, handler: {
                (action: UIAlertAction!) -> Void in
                
                let password = alert.textFields?.first?.text
                if password == getUserPojo().userpassword {
                    let changePassword = ChangePWDViewController()
                    self.navigationController?.pushViewController(changePassword, animated: true)
                }else{
                    let alert: UIAlertController = UIAlertController.init(title: nil, message: "密码错误，请重新输入。\n如果忘记密码，请联系学校管理员。", preferredStyle: UIAlertControllerStyle.alert)
                    let cancleAction = UIAlertAction.init(title: "知道了", style: UIAlertActionStyle.cancel, handler: nil)
                    alert.addAction(cancleAction)
                    self.present(alert, animated: true, completion: nil)
                    
                }
            })
            alert.addAction(cancleAction)
            alert.addAction(okAction)
            self.present(alert, animated: true, completion: nil)
            
            break
        default:
            break;
        }
    }
    
    func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
        if buttonIndex == 1 {
            hud = MBProgressHUD.showAdded(to: self.view, animated: true)
            hud.animationType = MBProgressHUDAnimation.fade
            hud.labelText = "正在清理缓存...请稍候"
            
            SDImageCache.shared().clearDisk(onCompletion: {
                self.hud.hide(true)
                self.hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                self.hud.mode = MBProgressHUDMode.customView
                self.hud.labelText = "缓存已清理！";
                self.hud.customView = UIImageView.init(image: UIImage.init(named: "okbai"))
                DispatchQueue.global( priority: DispatchQueue.GlobalQueuePriority.low).async(execute: {
                    // Do something...
                    sleep(1);
                    DispatchQueue.main.async(execute: {
                        self.hud.hide(true)
                        
                    });
                });
                
            })
            
        }
    }
    
    func getCachSize() -> CGFloat {
        
        let cachSize = CGFloat(SDImageCache.shared().getSize())/CGFloat(1024*1024)
        return cachSize
    }
    
    
    /**
     桐乡教育管家模块
     
     - parameter notification: 这个方法转移到了MainViewController 里面了
     */
    func MainRightClick(_ notification: Notification) {
        
        let dic: NSDictionary = (notification as NSNotification).userInfo! as NSDictionary
        let target = dic.object(forKey: "target") as! String
        let apiurl = dic.object(forKey: "apiurl") as! String
        
        switch target {
        case "1":
            /**
             *  班级活动
             */
            
            let classWork = ClassWorkViewController()
            classWork.apiurl = apiurl
            self.navigationController?.pushViewController(classWork, animated: true)
            break;
        case "2":
            /**
             *  班级荣誉
             */
            let honor = HonorViewController()
            self.navigationController?.pushViewController(honor, animated: true)
            break;
        case "3":
            /**
             *  班级通知
             */
            let notice = NoticeViewController()
            notice.modetype = target
            self.navigationController?.pushViewController(notice, animated: true)
            break;
        case "4":
            /**
             *  课程表
             */
            let course = CourseViewController()
            self.navigationController?.pushViewController(course, animated: true)
            break;
        case "5":
            /**
             *  桐乡教育统计
             */
            let bellStatisVC = BellStatisViewController()
            self.navigationController?.pushViewController(bellStatisVC, animated: true)
            break;
        case "6":
            /**
             *  家长关注
             */
            let parent = ParentFoucsViewController()
            self.navigationController?.pushViewController(parent, animated: true)
            break;
        case "7":
            /**
             *  成长记录
             */
            let growth = GrowthViewController()
            growth.apiurl = apiurl
            self.navigationController?.pushViewController(growth, animated: true)
            break;
        case "8":
            /**
             *  今日请假
             */
            let leave = LeaveViewController()
            self.navigationController?.pushViewController(leave, animated: true)
            break;
        case "9":
            /**
             *  今日考勤
             */
            let attendance = AttendanceViewController()
            self.navigationController?.pushViewController(attendance, animated: true)
            break;
        case "10":
            /**
             *  桐乡教育日报
             */
            let dayReport = DayReportViewController()
            dayReport.apiurl = apiurl
            self.navigationController?.pushViewController(dayReport, animated: true)
            break;
        case "11":
            /**
             *  作业备忘
             */
            let homeWork = HomeWorkViewController()
            self.navigationController?.pushViewController(homeWork, animated: true)
            break;
        case "12":
            /**
             *  问卷调查
             */
            let suvery = SuveryViewController()
            self.navigationController?.pushViewController(suvery, animated: true)
            break;
        case "13":
            /// 班级圈
            let classCircle = ClassCircleViewController()
            classCircle.apiurl = apiurl
            self.navigationController?.pushViewController(classCircle, animated: true)
            break

        case "14":
            // 全校通知
            #if EDUMANAGE || HSEDUMANAGE || TXEDUMANAGE
                
                let SchNotice = SchNoticeViewController()
                SchNotice.modetype = target
                self.navigationController?.pushViewController(SchNotice, animated: true)
            #else
                TSMessage.showNotification(in: self, title: "提示", subtitle: "对不起，该模块权限尚未开放！请稍等", image: nil, type: TSMessageNotificationType.warning, duration: 2.0, callback: nil, buttonTitle: "知道了", buttonCallback: nil, at: TSMessageNotificationPosition.top, canBeDismissedByUser: true)
            #endif
            break
        default:
            TSMessage.showNotification(in: self, title: "提示", subtitle: "对不起，该模块权限尚未开放！请稍等", image: nil, type: TSMessageNotificationType.warning, duration: 2.0, callback: nil, buttonTitle: "知道了", buttonCallback: nil, at: TSMessageNotificationPosition.top, canBeDismissedByUser: true)
            break;
        }
        
        
    }
 
  */
 
    /**
     tab选中视图
     
     - parameter tabBarController:
     - parameter viewController:
     */
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        let index = tabBarController.selectedIndex
        switch index {
        case 0:
            self.title = "办公管家"
            break;
        case 1:
            self.title = "家校管家"
            break;
        case 2:
            self.title = "我"
            break;
        default:
            break
        }
        
    }
    
 
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
